package com.feng.example;

import com.facebook.nifty.core.NettyServerTransport;
import com.facebook.nifty.core.ThriftServerDef;
import com.facebook.nifty.core.ThriftServerDefBuilder;

import org.apache.thrift.TProcessor;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;


public class UserServer {
    public static void StartsimpleServer(UserService.Processor processor) {
        try {
            TServerTransport serverTransport = new TServerSocket(9090);
            TServer server = new TSimpleServer(
                    new TServer.Args(serverTransport).processor(processor));
            System.out.println("Starting the simple server...");
            server.serve();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void startServer() {
        UserService.Iface serviceInterface = new UserServiceImpl();
        TProcessor processor = new UserService.Processor<>(serviceInterface);
        ThriftServerDef serverDef = new ThriftServerDefBuilder().withProcessor(processor).build();
        final NettyServerTransport server = new NettyServerTransport(serverDef);
        server.start();

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    server.stop();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });
    }

    public static void main(String[] args) {
//        StartsimpleServer(new UserService.Processor(new UserServiceImpl()));
        startServer();
    }
}